/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 *
 *MmsProvinceMarketingInfoController
 * 省区行销配置信息表新
 *
 * 修改纪录
 * 2021-07-08 版本：1.0 lvzhijun 创建。
 * @version 版本：1.0
 * @author 作者：lvzhijun
 * 创建日期：2021-07-08
 */

package com.topscomm.mms.controller;

import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.BasicMapperDao;
import com.topscomm.basic.IBasicMapper;
import com.topscomm.basic.IBasicService;
import com.topscomm.cbo.model.TreeModel;
import com.topscomm.cbo.pojo.CboItemDetailsEntity;
import com.topscomm.cbo.pojo.CboItemsEntity;
import com.topscomm.cbo.service.impl.CboItemDetailsService;
import com.topscomm.cbo.service.impl.CboItemsService;
import com.topscomm.mms.MmsSystemConst;
import com.topscomm.mms.controller.auto.MmsProvinceMarketingInfoControllerAuto;
import com.topscomm.mms.pojo.MmsDeptExtraInfoEntity;
import com.topscomm.mms.service.impl.MmsDeptExtraInfoService;
import com.topscomm.mms.service.impl.MmsProvinceCityMatchinfoService;
import com.topscomm.mms.service.impl.MmsProvinceMarketingInfoService;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.RedisDatacodeStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: lvzhijun
 * @date: 2021-07-08
 * @description:省区行销配置信息表新Controller
 * @modify:
 */
@RestController
@RequestMapping("mms/provinceMarketingInfo")
public class MmsProvinceMarketingInfoController extends MmsProvinceMarketingInfoControllerAuto implements IMmsProvinceMarketingInfoController {
	@Autowired
	MmsProvinceMarketingInfoService mmsProvinceMarketingInfoService;
    @Autowired
	MmsProvinceCityMatchinfoService mmsProvinceCityMatchinfoService;
	@Autowired
	CboItemDetailsService cboItemDetailsService;
	/**
	 * @author: lvzhijun
	 * @date: 2021-07-08
	 * @param paraMap
	 * @return sql query string
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}

		if (paraMap.containsKey("qry_marketingpost")) {
			String value = paraMap.get("qry_marketingpost").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and marketingpost = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_deptcommoncode1")) {
			String value = paraMap.get("qry_deptcommoncode1").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and deptcommoncode1 = '{0}' ", value);
		}
		//如果是运营人员，查所有，否则返回当前权限下的所有
		if(!checkIsOperators()) {
			sql.appendSingle(" and deptcommoncode1 in (SELECT  deptcommoncode from MMSDEPTEXTRAINFO  where PROVINCECHOCODE = '{0}' or PROVINCEMANAGERCODE = '{0}' or CFOCODE = '{0}' )",
					getUserBean().getUser().getCode());
		}

		return sql.toString();
	}

	/**
	 * @author: lvzhijun
	 * @date: 2021-07-08
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}
	/**
	 * @author: xinfushuo
	 * @date: 2021-07-26
	 * @param paraMap
	 * @return
	 * @description:插入新行销岗位
	 * @modify:
	 */
	@PostMapping(value="/addMarketingpost/insert")
	public ResponseResult<Object> addProvincePost(@RequestBody(required = false) Map<String,Object> paraMap){
	    try{
            CboItemDetailsEntity res = mmsProvinceMarketingInfoService.addProvincePost(paraMap);
	    	Map<String,Object> dataMap =res.convertToMap();
	    	return ResponseResult.ok(dataMap);
		}
	    catch (Exception ex){
	    	return handleControllerException(ex);
		}

	}

	/**
	 *
	 * @description: 判断是否为运营人员
	 * @param
	 * @return
	 * @author: hudeyong
	 * @date: 2021-06-24
	 * @modify:
	 */
	public Boolean checkIsOperators() {
		boolean key = false;
		Map<String, Object> paraMap = new HashMap<>();
		paraMap.put("code", MmsSystemConst.ItemsEnum.bspOperators);
		paraMap.put("usercode", getUserBean().getUser().getCode());
		int count = mmsProvinceCityMatchinfoService.getCount(paraMap, "getOperatorsCount");
		if(count>0) {
			return true;
		}
		return key;
	}
	/**
	 *
	 * @description:
	 * @param paraMap
	 * @return
	 * @author: lvzhijun
	 * @date: 2021-07-08
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/dataCodeList")
	public ResponseResult<Object> dataCodeList(@RequestParam(required = false) Map<String, Object> paraMap) {
		Map<String, Object> codeMap = new HashMap<String, Object>();
		StringBufferProxy sql = new StringBufferProxy();
		String userCode =  getUserBean().getUser().getCode();


		List<Map<String, Object>> deptcommoncode1List ;
		if(checkIsOperators()) {
			deptcommoncode1List = datacodeCache.getCodeListMap("DeptCommonCodeTrack", "", "sortcode");
			codeMap.put("deptcommoncode1List", deptcommoncode1List);
		}else{
			sql.appendSingle(" and code in (SELECT  deptcommoncode from MMSDEPTEXTRAINFO  where PROVINCECHOCODE = '{0}' or PROVINCEMANAGERCODE = '{0}' or CFOCODE = '{0}' )",
					getUserBean().getUser().getCode());
			List<CboItemDetailsEntity> items = cboItemDetailsService.queryByWhere(CboItemDetailsEntity.FieldItemid+" = "+" (SELECT id from CBOITEMS where code = 'DeptCommonCodeTrack') " +sql.toString());
			deptcommoncode1List=new ArrayList<Map<String,Object>>();
			for(CboItemDetailsEntity item :items){
						deptcommoncode1List.add(item.convertToMap());
			}
			codeMap.put("deptcommoncode1List", deptcommoncode1List);
		}
		List<Map<String, Object>> marketingpostList = datacodeCache.getCodeListMap("MmsMarketingPost", "", "sortcode");
		codeMap.put("marketingpostList", marketingpostList);
		List<Map<String, Object>> productLineList = datacodeCache.getCodeListMap("PostDeptCommonCode", "", "sortcode");
		codeMap.put("productLineList", productLineList);
		List<Map<String, Object>> enabledList = datacodeCache.getCodeListMap("CboYesNo", "", "sortcode");
		codeMap.put("enabledList", enabledList);
		super.dataCodeList(paraMap);
		return ResponseResult.ok(codeMap);
	}


}

